package com.tencent.service;

import com.tencent.Mapper.LoginMapper;
import com.tencent.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: health
 * @BelongsPackage: com.tencent.service
 * @ClassName: LoginDetailsService
 * @Description: TODO
 * @Version: 1.0.0
 * @CreateTime: 2022-08-12  14:02
 * @Author: Yangjx
 */
@Service
public class LoginDetailsService implements UserDetailsService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = loginMapper.findByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException("找不到输入的用户名,请重新输入。");

        }

        //查不到用户(用户名错误)会出现空指针，增加异常处理器捕获
        return org.springframework.security.core.userdetails.User.withUsername(user.getUsername())
                .password(user.getPassword())
                //先写死为default;  查询当前用户对应的角色权限,并在此赋值
                .authorities("default")
                .build();
    }
}
